package com.ckzp.jfinal.weixin.controller;

import com.ckzp.jfinal.base.BaseController;
import com.ckzp.jfinal.base.PageSearch;
import com.ckzp.jfinal.form.FormData;
import com.ckzp.jfinal.form.FormSelect;
import com.ckzp.jfinal.interceptor.Admins;
import com.ckzp.jfinal.weixin.model.WxMember;
import com.ckzp.jfinal.weixin.model.WxPay;
import com.ckzp.plugins.layui.LayuiData;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * Created by 51594 on 2020/6/1.
 */
public class WxPayController extends BaseController {

    @Admins(roles = "wxadmin", name = "默认", log = false)
    public void index() {
        renderText("默认");
    }


    /**
     * 微信支付流水PAGE
     */
    @Admins(roles = "wxadmin", name = "微信支付流水PAGE", log = false)
    public void WxPayList() {
        FormData fd = FormData.dao();
        setAttr("form", fd);
        render("WxPayList.html");
    }

    /**
     * 微信支付流水查询API
     */
    @Admins(roles = "wxadmin", name = "微信支付流水查询API", log = false)
    public void WxPayListDataApi() {
        PageSearch ps = PageSearch.n()
                .setTable("wx_pay")
                .orderby("pay_id desc")
                .initSearch(getRequest());
        Page<WxPay> pages = ps.paginate(WxPay.dao);
//        String sql = "from wx_pay ";
//        sql = addSearch(sql, "pay_id", "=", "string", getPara("s[pay_id]"));
//        sql = addOrderBy(sql, "pay_id");
//        Page<WxPay> pages = WxPay.dao.paginate(getPage(), getLimit(10), "select *", sql);
        List<WxPay> datalist = pages.getList();
        FormSelect fs = FormSelect.n().setSource("``未支付`SUCCESS`支付成功`FAIL`支付失败`").setReadOnly(true);
        for (WxPay pay : datalist) {
            pay.put("pre_fee_d", (double) pay.mod.getInt("pre_fee", 0) / 100);
            pay.put("total_fee_d", (double) pay.mod.getInt("total_fee", 0) / 100);
            pay.put("result_code_s", fs.showHtml(pay.mod.getStr("result_code")));
        }
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()));
    }
}